package com.ruoyi.business.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.business.domain.ZrsMbManage;
import com.ruoyi.business.service.IZrsMbManageService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 责任书模版管理Controller
 * 
 * @author Yuhf
 * @date 2023-11-18
 */
@RestController
@RequestMapping("/business/ZrsMbManage")
public class ZrsMbManageController extends BaseController
{
    @Autowired
    private IZrsMbManageService zrsMbManageService;

    /**
     * 查询责任书模版管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZrsMbManage zrsMbManage)
    {
        startPage();
        List<ZrsMbManage> list = zrsMbManageService.selectZrsMbManageList(zrsMbManage);
        return getDataTable(list);
    }

    /**
     * 导出责任书模版管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:export')")
    @Log(title = "责任书模版管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZrsMbManage zrsMbManage)
    {
        List<ZrsMbManage> list = zrsMbManageService.selectZrsMbManageList(zrsMbManage);
        ExcelUtil<ZrsMbManage> util = new ExcelUtil<ZrsMbManage>(ZrsMbManage.class);
        util.exportExcel(response, list, "责任书模版管理数据");
    }
    
    /**
     * 选择模版目标项管理记录
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:list')")
    @PostMapping(value = "/getZrsMbManageSelKhDept")
    public AjaxResult getZrsMbManageSelKhDept(@RequestBody ZrsMbManage zrsMbManage)
    {
    	//zrsMbManageGoalService.updateZrsMbManageGoal(zrsMbManageGoal)
    	//首先选择了那些  管理项指标
    	Map<String,Object> date = new HashMap<String,Object>();
    	date.put("khDept", zrsMbManageService.selectZbManageKhDeptAll());
    	date.put("selKhDeptVal", zrsMbManageService.selectSelZbManageKhDeptListByMbId(zrsMbManage));
    	
        return AjaxResult.success(date);
    }
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:edit')")
    @Log(title = "责任书模版被考核部门关系保存", businessType = BusinessType.UPDATE)
    @PostMapping("/updateZrsMbManageShDeptToSaveDept")
    public AjaxResult updateZrsMbManageShDeptToSaveDept(@RequestBody ZrsMbManage zrsMbManage)
    {
    	
    	zrsMbManage.setUpdateTime(DateUtils.getNowDate());
    	zrsMbManage.setUpdateBy(getUsername());
        return toAjax(zrsMbManageService.updateZrsMbManageShDeptToSaveDept(zrsMbManage));
    }
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:list')")
    @PostMapping(value = "/getZrsMbManageSelKhUser")
    public AjaxResult getZrsMbManageSelKhUser(@RequestBody ZrsMbManage zrsMbManage)
    {
    	//首先选择了那些  管理项指标
    	Map<String,Object> date = new HashMap<String,Object>();
    	date.put("khUser", zrsMbManageService.selectZbManageKhUserAll());
    	date.put("selKhUserVal", zrsMbManageService.selectSelZbManageKhUserListByMbId(zrsMbManage));
    	
        return AjaxResult.success(date);
    }
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:edit')")
    @Log(title = "责任书模版被考核人员关系保存", businessType = BusinessType.UPDATE)
    @PostMapping("/updateZrsMbManageShUserToSaveUser")
    public AjaxResult updateZrsMbManageShUserToSaveUser(@RequestBody ZrsMbManage zrsMbManage)
    {
    	
    	zrsMbManage.setUpdateTime(DateUtils.getNowDate());
    	zrsMbManage.setUpdateBy(getUsername());
    	return toAjax(zrsMbManageService.updateZrsMbManageShUserToSaveUser(zrsMbManage));
    }
    
    /**
     * 获取责任书模版管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:query')")
    @GetMapping(value = "/{mbId}")
    public AjaxResult getInfo(@PathVariable("mbId") Long mbId)
    {
        return success(zrsMbManageService.selectZrsMbManageByMbId(mbId));
    }

    /**
     * 新增责任书模版管理
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:add')")
    @Log(title = "责任书模版管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZrsMbManage zrsMbManage)
    {
    	zrsMbManage.setCreateBy(getUsername());
        return toAjax(zrsMbManageService.insertZrsMbManage(zrsMbManage));
    }

    /**
     * 修改责任书模版管理
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:edit')")
    @Log(title = "责任书模版管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZrsMbManage zrsMbManage)
    {
    	zrsMbManage.setUpdateBy(getUsername());
    	zrsMbManage.setUpdateTime(DateUtils.getNowDate());
        return toAjax(zrsMbManageService.updateZrsMbManage(zrsMbManage));
    }
    /**
     * 责任书模版管理  应用
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:edit')")
    @Log(title = "责任书模版管理-应用", businessType = BusinessType.UPDATE)
	@GetMapping("/editEnable/{mbId}")
    public AjaxResult editEnable(@PathVariable Long mbId)
    {
    	ZrsMbManage zrsMbManage = new ZrsMbManage();
    	zrsMbManage.setUpdateBy(getUsername());
    	zrsMbManage.setUpdateTime(DateUtils.getNowDate());
    	zrsMbManage.setMbId(mbId);
    	zrsMbManage.setIsEnable("1");//0未使用  1 已使用
    	//判断是否可以应用
    	ZrsMbManage info = zrsMbManageService.selectZrsMbManageByMbId(mbId);
    	if(info != null && "1".equals(info.getMbType())) {/** 1部门0人员 */
    		int r = zrsMbManageService.selectSelZbManageKhDeptCountByMbId(mbId);
    		if(r == 0) {
    			return AjaxResult.error("责任书模版没有配置适用部门或者人员！无法进行应用！");
    		}
    	}else if(info != null && "0".equals(info.getMbType())) {/** 1部门0人员 */
    		int r = zrsMbManageService.selectSelZbManageKhUserCountByMbId(mbId);
    		if(r == 0) {
    			return AjaxResult.error("责任书模版没有配置适用部门或者人员！无法进行应用！");
    		}
    	}
        return toAjax(zrsMbManageService.updateZrsMbManage(zrsMbManage));
    }
    
    /**
     * 责任书模版管理  应用
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:edit')")
    @Log(title = "责任书模版管理-取消应用", businessType = BusinessType.UPDATE)
	@GetMapping("/editUnEnable/{mbId}")
    public AjaxResult editUnEnable(@PathVariable Long mbId)
    {
    	ZrsMbManage zrsMbManage = new ZrsMbManage();
    	zrsMbManage.setUpdateBy(getUsername());
    	zrsMbManage.setUpdateTime(DateUtils.getNowDate());
    	zrsMbManage.setMbId(mbId);
    	zrsMbManage.setIsEnable("0");//0未使用  1 已使用
        return toAjax(zrsMbManageService.updateZrsMbManage(zrsMbManage));
    }
    /**
     * 责任书模版管理  应用
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:edit')")
    @Log(title = "责任书模版管理-复制", businessType = BusinessType.UPDATE)
	@GetMapping("/copyEdit/{mbId}")
    public AjaxResult copyEdit(@PathVariable Long mbId) {
    	
        return toAjax(zrsMbManageService.copyEdit(mbId));
    }
    
    
    /**
     * 删除责任书模版管理
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:remove')")
    @Log(title = "责任书模版管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{mbId}")
    public AjaxResult remove(@PathVariable Long mbId)
    {
    	ZrsMbManage zrsMbManage = new ZrsMbManage();
    	zrsMbManage.setDelBy(getUsername());
    	zrsMbManage.setDelTime(DateUtils.getNowDate());
    	zrsMbManage.setMbId(mbId);
    	zrsMbManage.setDelFlag("2");
        return toAjax(zrsMbManageService.updateZrsMbManage(zrsMbManage));
    }
    /**
     * 删除责任书模版管理
     */
    @PreAuthorize("@ss.hasPermi('business:ZrsMbManage:remove')")
    @Log(title = "责任书模版管理", businessType = BusinessType.UPDATE)
	@PostMapping("/generateZrsManage")
    public AjaxResult generateZrsManage(@RequestBody ZrsMbManage zrsMbManage)
    {
    	zrsMbManage.setCreateBy(getUsername());
    	zrsMbManage.setCreateTime(DateUtils.getNowDate());
    	zrsMbManageService.generateZrsManage(zrsMbManage);
    	
        return toAjax(1);
    }
    
}
